package ai.attackControllers;

import ai.AITools;
import ai.AbstractPilotController;

import com.jme.math.Vector3f;

import logic.common.player.Player;
import logic.nodes.TeamNode;
import logic.ships.carrier.Carrier;
import logic.ships.hunter.Hunter;
import logic.weapons.WeaponType;


/**
 * This is an {@link AbstractPilotController} that attacks the {@link Player}'s target. If this controller 
 * is attached to the {@link Hunter}, the {@link Hunter} will fly towards the target and fire if it's 
 * within shoot-range. If it's not within shoot-range or not visible, boost will be enabled.
 * 
 * @author Wasserleiche
 */
public abstract class AttackController extends AbstractPilotController {
	
	private static final long serialVersionUID = 1L;
	
	/** Determines the distance to the target within the pilot disables boost and tries to fire. */
	private final float SHOOT_DIST = 300f;
	
	/**
	 * Constructor for AttackController.
	 * @param bot The {@link Player} to be controlled.
	 */
	public AttackController(Player bot) {
		super(bot);	
	}
	
	@Override
	public void delayedUpdate(float time) {
		if(!checkAIEnabled()) return;
		
		super.delayedUpdate(time);
		
		bot.getHunter().getMoveForwardController().stop(false);
		
		TeamNode target = bot.getBotTarget();
		if(target == null) return;
		
		rotateTo(target.getLocalTranslation(), agility * time, false);
		
		if(target.isDestroyed()) return;
		
		boolean isVisible = AITools.isLookingTo(bot.getHunter(), target);
		if(SHOOT_DIST > getTargetDistance() && isVisible) {
			boost(false);
			fireWeapons();
		} else if(isVisible) boost(true);
	}
	
	protected void fireWeapons() {
		Hunter hunter = bot.getHunter();
		TeamNode target = bot.getBotTarget();
		
		if(AITools.isVisible(hunter, target)) fireWeapons(hunter, target);
	}
	
	protected abstract void fireWeapons(Hunter hunter, TeamNode target);
	
	protected void fireHeatSeeker(Vector3f dir) {
		if(bot.getBotTarget() instanceof Hunter) bot.getHunter().fireWeapons(dir, WeaponType.HeatSeeker);
	}
	
	protected void fireMissiles(Vector3f dir) {
		TeamNode target = bot.getBotTarget();
		if(!target.canMove() || (target instanceof Carrier && ((Carrier)target).getFrontSpeed() == 0f)) 
			bot.getHunter().fireWeapons(dir, WeaponType.Missile);
	}
}